﻿/*
 *  ChgDoc CMS - GNU General Public License, version 3 (GPL-3.0)
 *  Copyright (C)  2012. Ian Quigley
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ChgDoc.Entity;
using ChgDoc.Edit.PageRenderControls;

namespace ChgDoc.Edit
{
	[AdminPage( "Edit Page", "Admin" )]
	public partial class PageEdit : BasePage
	{
		protected override IEnumerable<FormRule> GetFormRules()
		{
			return new List<FormRuleMapping<PageResponse>>()
			       	{
			       		new FormRuleMapping<PageResponse>( 
                                txtUrl, "Url", FormRuleCondition.NotNull,
                                   (s,v) => s.Url = (string)v )
			       	};
		}

		protected void Page_Load( object sender, EventArgs e )
		{
			if (IsPostBack)
				return;

			drpRender.DataSource = Enum.GetNames( typeof (PageRender) );
			drpRender.DataBind();

			SetTitle( "Create page" );
            GetPageRenderControl();
            
			if (!HasParam("Id") && !HasParam("Url") )
				return;

			SetTitle( "Edit page" );
			
			PageResponse page = HasParam( "Id" )
			                    	? PageResponse.Read( LongParam( "Id" ) )
			                    	: PageResponse.Read( Param( "Url" ) );
			ToPage( page );
		}

		private void ToPage(PageResponse page)
		{
			if (Param( "Url" ) != null)
				txtUrl.Text = Param( "Url" );

			if (page == null)
				return;

            drpRender.SelectedValue = page.Render.ToString();
	
            GetPageRenderControl().ValuesString = page.RenderOptions;

			txtUrl.Text = page.Url;
		}

        private PageRender SelectedRenderer()
        {
            return (PageRender)Enum.Parse(typeof(PageRender), drpRender.SelectedItem.Text);
        }

		protected void btnSave_click(object sender, EventArgs e)
		{
            PageResponse page = new PageResponse()
            {
                Id = LongParam("Id"),
                Render = SelectedRenderer(),
                RenderOptions = GetPageRenderControl().ValuesString
            };

            if (!PopulateModelFromControls<PageResponse>(page))
                return;

			page.Save();
            DoReturnUrl();
		}
        
        protected void drpRender_Change(object sender, EventArgs e)
        {
            GetPageRenderControl();
        }

        private void HideAllPageRenderControls()
        {
            ucRedirect.Visible = false;
            ucTemplate.Visible = false;
            ucTemplateData.Visible = false;
            ucFileResponse.Visible = false;
            ucTemplateSql.Visible = false;
        }

        private PageRenderControl GetPageRenderControl()
        {
        	HideAllPageRenderControls();

			PageRenderControl control;
            switch (SelectedRenderer())
            {
                case PageRender.Redirect:
					control =ucRedirect;
            		break;

                case PageRender.Template:
                    control = ucTemplate;
                    break;

				case PageRender.TemplateData:
					control = ucTemplateData;
					break;
				
				case PageRender.FileResponse:
					control = ucFileResponse;
					break;

                case PageRender.TemplateSql:
                    control = ucTemplateSql;
                    break;
                   
                default:
                    throw new NotImplementedException();
            }

        	control.Visible = true;
        	return control;
        }

	}
}